import { UserProfilesAPI } from "@/request/api"
export default {
    namespaced: true,
    state: {
        userInfo: {
            nickName: '',
            coin: 0,
            headImg: require("../../assets/img/userImg.f8bbec5e.png")
        },
        cartTotal: 0
    },
    mutations: {
        changeUserInfo(state, payload) {
            console.log("修改vuex中用户信息", payload);
            state.userInfo = payload.userInfo
            state.cartTotal = payload.cartTotal
        },
        initUserInfo(state) {
            state.userInfo = {
                nickName: '',
                coin: 0,
                headImg: require("../../assets/img/userImg.f8bbec5e.png")
            }
            state.cartTotal = 0
        }
    },
    actions: {
        async asyncChangeUserInfo(context) {
            let user = await UserProfilesAPI()
            console.log("user:", user.data);
            context.commit("changeUserInfo", user.data)
        }
    }
}
/* 
    1、刷新之后vuex数据还原到默认状态
       原因：vuex缺点，刷新后，会还原默认状态，数据没有持久化
       持久化：存储在数据库，本地存储
       解决：
       1、重新请求数据库数据
       2、存储到本地 localStorage

       分析：刷新-->topbar.vue,created(),else代码，更新用户状态处，已登录重新请求用户信息
*/